package com.tyj.tuyijieapp.controller.exception;


import com.tyj.tuyijiecommon.exception.CodeException;
import com.tyj.tuyijiecommon.exception.ExpirationException;
import com.tyj.tuyijiecommon.exception.UpdateException;
import com.tyj.tuyijiecommon.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 全局异常统一处理
 * </p>
 *
 * @author lhf
 * @since 2020/6/8
 */
@RestControllerAdvice
@ApiIgnore
public class GlobalExceptionHandler {
    private static Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 登录超时，token过期
     *
     * @param e
     * @return
     */
    @ExceptionHandler(ExpirationException.class)
    public Object expirationException(ExpirationException e) {
        return Result.builder().code(Result.ResultCode.TOKEN_EX.getCode()).mes(Result.ResultCode.TOKEN_EX.getMes()).build();
    }

    /**
     * 验证码错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(CodeException.class)
    public Object codeException(CodeException e) {
        return Result.builder().code(Result.ResultCode.CODE_EX.getCode()).mes(Result.ResultCode.CODE_EX.getMes()).build();
    }

    /**
     * 更新失败
     *
     * @param e
     * @return
     */
    @ExceptionHandler(UpdateException.class)
    public Object updateException(UpdateException e) {
        return Result.failure("更新失败");
    }

    /**
     * 其他异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Object exception(Exception e) {
        log.error("服务端异常:{}", e.getMessage());
        return Result.failure("服务端异常");
    }
}
